package com.edu.service.impl;

import com.edu.dao.EduDeptMapper;
import com.edu.dao.EduRoleMapper;
import com.edu.dao.EduStuMapper;
import com.edu.dao.EduTeacherMapper;
import com.edu.pojo.EduDept;
import com.edu.pojo.EduTeacher;
import com.edu.service.EduDeptService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 分院/班级服务层
 *
 * @author YJF
 */
@Service
public class EduDeptServiceImpl implements EduDeptService {

    @Autowired
    private EduDeptMapper eduDeptMapper;

    @Autowired
    private EduStuMapper eduStuMapper;

    @Autowired
    private EduTeacherMapper eduTeacherMapper;

    @Override
    public List<EduDept> findAll() {
        return eduDeptMapper.selectAll();
    }

    @Override
    public PageInfo<EduDept> findByPage(String page, String size, String query) {
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(size));
        EduDept eduDept = new EduDept();
        eduDept.setDeptName(query);
        Example example = createExample(eduDept);
        List<EduDept> deptList = eduDeptMapper.selectByExample(example);
        for (EduDept dept : deptList) {
            dept.setLeader(eduTeacherMapper.findLeader(dept.getTecId()));
            Integer deptId = dept.getDeptId();
            dept.setStuCount(eduStuMapper.selectStuCount(deptId));
            dept.setTecCount(eduTeacherMapper.selectTecCount(deptId));
            EduDept child = new EduDept();
            child.setParentId(deptId);
            List<EduDept> children = eduDeptMapper.select(child);
            if (children != null && children.size() > 0) {
                dept.setChildren(children);
            }
        }
        return new PageInfo<>(deptList);
    }

    @Override
    public int addDept(EduDept eduDept) {
        if (eduDeptMapper.selectByName(eduDept.getDeptName()) > 0) {
            return -1;
        }
        EduTeacher teacher = getEduTeacher(eduDept);
        if (teacher == null) {
            return 0;
        }
        eduDept.setTecId(teacher.getTecId());
        eduDept.setStatus("1");

        if (eduDept.getParentId() == null) {
            eduDept.setParentId(0);
        }

        Date date = new Date();
        eduDept.setCreateTime(date);
        eduDept.setUpdateTime(date);
        eduDept.setCreateBy(eduDept.getCreateBy());
        eduDept.setUpdateBy(eduDept.getCreateBy());

        eduDeptMapper.insertSelective(eduDept);
        teacher.setDeptId(eduDept.getDeptId());
        if (eduDept.getParentId() == 0) {
            eduTeacherMapper.updateByPrimaryKeySelective(teacher);
        }
        return 1;
    }

    @Override
    public EduDept findById(Integer deptId) {
        return eduDeptMapper.selectById(deptId);
    }

    @Override
    public int update(EduDept eduDept) {
        EduTeacher teacher = getEduTeacher(eduDept);
        if (teacher == null) {
            return 0;
        }
        eduDept.setTecId(teacher.getTecId());

        Date date = new Date();
        eduDept.setUpdateTime(date);
        eduDept.setUpdateBy(eduDept.getUpdateBy());

        eduDeptMapper.updateByPrimaryKeySelective(eduDept);
        if (eduDept.getParentId() == 0) {
            teacher.setDeptId(eduDept.getDeptId());
            eduTeacherMapper.updateByPrimaryKeySelective(teacher);
        }
        return 1;
    }

    @Override
    public int delById(Integer deptId) {
        return eduDeptMapper.deleteByPrimaryKey(deptId);
    }

    @Override
    public PageInfo<EduDept> findClsPage(String page, String size, String parent, String cls) {
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(size));
        List<EduDept> deptList = eduDeptMapper.findCls(parent, cls);
        return new PageInfo<>(deptList);
    }

    @Override
    public EduDept findClsById(Integer deptId) {
        EduDept eduDept = eduDeptMapper.selectByPrimaryKey(deptId);
        eduDept.setParentDept(eduDeptMapper.selectByPrimaryKey(eduDept.getParentId()).getDeptName());
        try {
            EduTeacher eduTeacher = eduTeacherMapper.selectByPrimaryKey(eduDept.getTecId());
            eduDept.setLeader(eduTeacher.getTecName());
            eduDept.setLeaderId(eduTeacher.getTecPerId());
        } catch (Exception e) {
            System.out.println("无教师信息");
        }
        return eduDept;
    }

    @Override
    public int findStuCount(Integer deptId) {
        return eduStuMapper.findStuCountOnCls(deptId);
    }

    @Override
    public List<EduDept> findAllDept() {
        return eduDeptMapper.findAllDept();
    }

    @Override
    public List<EduDept> findAllClsById(Integer deptId) {
        return eduDeptMapper.findAllClsById(deptId);
    }

    /**
     * 查询教师信息
     */
    private EduTeacher getEduTeacher(EduDept eduDept) {
        EduTeacher leader = new EduTeacher();
        leader.setTecPerId(eduDept.getLeaderId());
        leader.setTecName(eduDept.getLeader());
        return eduTeacherMapper.selectOne(leader);
    }

    /**
     * 分院查询条件构建
     *
     * @param eduDept 查询条件
     */
    public Example createExample(EduDept eduDept) {
        Example example = new Example(EduDept.class);
        Example.Criteria criteria = example.createCriteria();
        if (eduDept != null) {
            if (!StringUtils.isEmpty(eduDept.getDeptName())) {
                criteria.andLike("deptName", "%" + eduDept.getDeptName() + "%");
            }
            criteria.andEqualTo("parentId", 0);
        }
        return example;
    }
}
